package net.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * UDP发送端
 * @author Administrator
 *
 */
public class UDPSender {
	
	public static void main(String[] args) throws IOException {
			
		System.out.println("发送端。。。。。。。。。。。。。");
		
		// 快递承包商
		DatagramSocket socket = new DatagramSocket();
		
		// 准备包裹
		String content = "我是一条消息";
		byte[] bytes = content.getBytes();
		InetAddress address = InetAddress.getByName("localhost");
		/*
		 * buf:数据
		 * offset:数据从哪个地方开始
		 * length:数据的长度
		 * address:地址
		 * port:端口
		 * */
		DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, address, 8888);
		
		// 邮寄
		socket.send(packet);
		
		// 善后工作：付邮费
		socket.close();
		
		System.out.println("发送完成。。。。。。。。。。。。。");
		
	}
	
}
